package com.wish.house.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.wish.common.security.annotation.RequiresPermissions;
import com.wish.house.entity.City;
import com.wish.house.service.ICityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 三级联动 前端控制器
 * </p>
 *
 * @author hyx
 * @since 2022-11-23
 */
@RestController
@RequestMapping("/city")
public class CityController {

    @Autowired
    ICityService cityService;

    @GetMapping("/list")
    //@RequiresPermissions("house:city:list")
    public List<Tree<String>>  list() {
        List<City> nodeList = cityService.list();
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都要默认值的
        treeNodeConfig.setIdKey("id");
        // 最大递归深度
        treeNodeConfig.setDeep(3);

        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(nodeList, "1", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getId().toString());
                    tree.setParentId(treeNode.getPid().toString());
                    tree.setName(treeNode.getName());
                    // 扩展属性 ...
                    tree.putExtra("label", treeNode.getName());
                    tree.putExtra("value", treeNode.getId());
                });
        return treeNodes;
    }

}
